package cn.edu.neusoft.food729.model;

import cn.edu.neusoft.food729.Bean.CollectBean;
import cn.edu.neusoft.food729.iface.Collectiface;
import cn.edu.neusoft.food729.listener.CollectListener;
import cn.edu.neusoft.food729.service.CollectService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class CollectModel implements Collectiface {
    private Retrofit retrofit;

    public CollectModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.130:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void collectShop(String user_id, int shop_id, final CollectListener listener) {
        CollectService collectService = retrofit.create(CollectService.class);
        Call<CollectBean> call = collectService.collectShop(user_id, shop_id);
        call.enqueue(new Callback<CollectBean>() {
            @Override
            public void onResponse(Call<CollectBean> call, Response<CollectBean> response) {
                if (response.isSuccessful() && response != null && response.body().getSuccess().equals("1")) {
                    listener.onResponse("collect");
                } else {
                    listener.onFail("收藏失败");
                }
            }

            @Override
            public void onFailure(Call<CollectBean> call, Throwable t) {
                listener.onFail("网路收藏错误:" + t.toString());
            }
        });

    }

    @Override
    public void collectFood(String user_id, int food_id, final CollectListener listener) {
        CollectService collectService = retrofit.create(CollectService.class);
        Call<CollectBean> call = collectService.collectFood(user_id, food_id);
        call.enqueue(new Callback<CollectBean>() {
            @Override
            public void onResponse(Call<CollectBean> call, Response<CollectBean> response) {
                if (response.isSuccessful() && response != null && response.body().getSuccess().equals("1")) {
                    listener.onResponse("collect");
                } else {
                    listener.onFail("收藏失败");
                }
            }

            @Override
            public void onFailure(Call<CollectBean> call, Throwable t) {
                listener.onFail("网路收藏错误:" + t.toString());
            }
        });
    }

    @Override
    public void exists(String user_id, int shop_food_id, int flag, final CollectListener listener) {
        CollectService collectService = retrofit.create(CollectService.class);
        Call<CollectBean> call = collectService.exists(user_id, shop_food_id, flag);
        call.enqueue(new Callback<CollectBean>() {
            @Override
            public void onResponse(Call<CollectBean> call, Response<CollectBean> response) {
                if (response.isSuccessful() && response != null && response.body().getCollected().equals("1")) {
                    listener.onResponse("collected");
                } else {
                    listener.onFail("unCollect");
                }
            }

            @Override
            public void onFailure(Call<CollectBean> call, Throwable t) {
                listener.onFail("网路判断收藏错误:" + t.toString());
            }
        });
    }

    @Override
    public void uncollectShop(String user_id, int shop_id, final CollectListener listener) {
        CollectService collectService = retrofit.create(CollectService.class);
        Call<CollectBean> call = collectService.collectShop(user_id, shop_id);
        call.enqueue(new Callback<CollectBean>() {
            @Override
            public void onResponse(Call<CollectBean> call, Response<CollectBean> response) {
                if (response.isSuccessful() && response != null && response.body().getSuccess().equals("1")) {
                    listener.onResponse("collectDelete");
                } else {
                    listener.onFail("收藏失败");
                }
            }

            @Override
            public void onFailure(Call<CollectBean> call, Throwable t) {
                listener.onFail("网路收藏错误:" + t.toString());
            }
        });
    }

    @Override
    public void uncollectFood(String user_id, int food_id, final CollectListener listener) {
        CollectService collectService = retrofit.create(CollectService.class);
        Call<CollectBean> call = collectService.collectFood(user_id, food_id);
        call.enqueue(new Callback<CollectBean>() {
            @Override
            public void onResponse(Call<CollectBean> call, Response<CollectBean> response) {
                if (response.isSuccessful() && response != null && response.body().getSuccess().equals("1")) {
                    listener.onResponse("collectDeleteFood");
                } else {
                    listener.onFail("收藏失败");
                }
            }

            @Override
            public void onFailure(Call<CollectBean> call, Throwable t) {
                listener.onFail("网路收藏错误:" + t.toString());
            }
        });
    }
}
